class Timer {
    constructor(option = {}) {
        const { template = 'h:m:s', step = 1000 } = option;
        this.timestamp = 0;
        this.timer = null;
        this.template = template;
        this.step = step;
    }

    render() {
        this.timestamp += this.step;
    }

    stop() {
        this.timestamp = 0;
        this.pause();
    }

    start() {
        this.stop();
        this.resume();
    }

    pause() {
        clearInterval(this.timer);
    }

    resume() {
        this.render();
        this.timer = setInterval(this.render.bind(this), this.step);
    }

    getFormatTime() {
        const seconds = parseInt(this.timestamp / 1000, 10);

        const s = parseInt(seconds % 60, 10);
        const m = parseInt((seconds / 60) % 60, 10);
        const h = parseInt((seconds / 3600) % 24, 10);

        const ary = [];
        ary.push(h < 10 ? `0${h}` : h);
        ary.push(m < 10 ? `0${m}` : m);
        ary.push(s < 10 ? `0${s}` : s);

        return ary.join(':');
    }

    getTimestamp() {
        return this.timestamp;
    }
}

export default Timer;
